from django.middleware.csrf import get_token
from django.shortcuts import render

from yh_backend.settings import BaseUrl
from api.models import NyCode,NyCodeType

# Create your views here.


def home_page(request):
    content = {
        'msg':'银河先生'
    }
    return render(request,'home_page.html',context=content)


def test(request):
    return render(request,'system/test.html')

def nyCode(request):
    # print(BaseUrl)
    TypeQueryset = NyCodeType.objects.all()
    content = {
        'BaseUrl':BaseUrl,
        "TypeQueryset": TypeQueryset,
    }
    return render(request, 'system/nycode/index.html',context=content)

def nyCodeAdd(request):
    # 获取 CSRF Token
    csrf_token = get_token(request)

    TypeQueryset = NyCodeType.objects.all()
    # print(BaseUrl)
    content = {
        'BaseUrl':BaseUrl,
        "TypeQueryset":TypeQueryset,
        # "csrf_token":csrf_token
    }
    return render(request, 'system/nycode/add.html',context=content)

def nyCodeEdit(request):
    # print(BaseUrl)
    id = request.GET.get('id')
    CodeQueryset = NyCode.objects.filter(id=id)
    TypeQueryset = NyCodeType.objects.all()
    content = {
        "BaseUrl":BaseUrl,
        "CodeQueryset":CodeQueryset,
        "TypeQueryset":TypeQueryset
    }
    return render(request, 'system/nycode/edit.html',context=content)


def nyCodeType(request):
    # print(BaseUrl)
    TypeQueryset = NyCodeType.objects.all()
    content = {
        'BaseUrl':BaseUrl,
        "TypeQueryset": TypeQueryset,
    }
    return render(request, 'system/nycodetype/index.html',context=content)

def nyCodeTypeAdd(request):
    # 获取 CSRF Token
    csrf_token = get_token(request)

    TypeQueryset = NyCodeType.objects.all()
    # print(BaseUrl)
    content = {
        'BaseUrl':BaseUrl,
        "TypeQueryset":TypeQueryset,
        # "csrf_token":csrf_token
    }
    return render(request, 'system/nycodetype/add.html',context=content)

def nyCodeTypeEdit(request):
    # print(BaseUrl)
    id = request.GET.get('id')
    print(id)
    NyCodeTypeSet = NyCodeType.objects.filter(id=id)
    print(NyCodeTypeSet)
    # TypeQueryset = NyCodeType.objects.all()
    content = {
        "BaseUrl":BaseUrl,
        "NyCodeTypeSet":NyCodeTypeSet,
        # "TypeQueryset":TypeQueryset
    }
    return render(request, 'system/nycodetype/edit.html',context=content)